<template>
  <!-- 模态框宽度 -->
  <el-dialog
    :title="dialogProps.title"
    :visible.sync="dialogProps.visible"
    width="500px"
    @open="handleDialogOpen()"
  >
    <div slot="title" class="dialog-header">{{ dialogProps.title }}</div>

    <el-form
      :model="ResetModel"
      :rules="formDietary"
      ref="SetMealForm"
      label-width="110px"
      label-position="right"
      size="small"
    >
      <el-row>
        <el-form-item label="预约日期" prop="time">
          <el-input
            v-model="ResetModel.time"
            ref="time"
            style="width:200px;"
          ></el-input>
        </el-form-item>

        <el-form-item label="可预约人数" prop="number">
          <el-input
            v-model="ResetModel.number"
            ref="number"
            style="width:200px;"
          ></el-input>
        </el-form-item>
      </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" size="small" @click="submitForm('SetMealForm')"
        >保 存</el-button
      >
      <el-button size="small" @click="handleDialogClose()">取 消</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { addSetmeal, updateSetMeal } from "@/api/Appointment/tsetmeal";
import RolePermission from "@/components/role/rolePermission";

export default {
  name: "role-edit",
  data() {
    return {
      activeName: "first",
      teamList: [],

      ResetModel: [
        {
          id: "",
          time: "",
          mon: "",
          sun: "",
          number: ""
        }
      ],

      //加载
      locked: false,

      dialogProps: {
        visible: false,
        action: "",
        title: ""
      },
      formDietary: {
        code: [{ required: true, message: "请输入套餐编码", trigger: "blur" }],
        name: [{ required: true, message: "请输入套餐名称", trigger: "blur" }]
      }
    };
  },
  methods: {
    handleDialogOpen() {
      this.$nextTick(() => {
        // this.$refs["SetMealForm"].clearValidate();
        // this.$refs["code"].focus();
      });
    },

    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          if (this.dialogProps.action === "add") {
            this.doSaveSetmeal();
          } else {
            this.doUpdateSetmeal();
          }
        } else {
          return false;
        }
      });
    },

    doUpdateSetmeal() {
      updateSetMeal(this.ResetModel)
        .then(response => {
          this.dialogProps.visible = false;
          if (response.data > 0) {
            this.$emit("save-finished");
            this.$message({
              message: "修改成功",
              type: "success"
            });
          }
        })
        .catch(error => {
          this.$message.error("修改失败");
        });
    },

    //设置
    doSaveSetmeal(ResetModel) {
      //   alert(this.ResetModel.number);
      //   alert(JSON.stringify(this.ResetModel));
      this.teamList = this.ResetModel.time.split("-");
      this.ResetModel.mon = this.teamList[1];
      this.ResetModel.sun = this.teamList[2];
      this.locked = false;
      this.dialogProps.visible = false;
      this.$emit("save-finished", this.ResetModel);
    },

    handleDialogClose() {
      this.dialogProps.visible = false;
    },

    outputError(error) {
      console.log(error.response ? error.response : error.message);
      this.$message({
        showClose: true,
        message: "出错了，请按F12查看浏览器日志。",
        type: "error"
      });
    }
  },

  mounted: function() {
    this.$nextTick(() => {
      this.$on("openEditDietaryDialog", function(eModel) {
        this.ResetModel = eModel;
        this.dialogProps.action = "edit";
        this.dialogProps.title = "编辑";
        this.dialogProps.visible = true;
      });
      this.$on("openAddReservationDialog", function(data) {
        this.dialogProps.action = "add";
        this.dialogProps.title = "新增";
        this.ResetModel = {
          time: data
        };
        this.dialogProps.visible = true;
      });
    });
  }
};
</script>

<style>
.el-upload-list__item-name {
  display: none;
}
</style>